package me.zhengjie.dailyPoem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.dailyPoem.domain.DailyPoemDiscussion;
import me.zhengjie.dailyPoem.domain.vo.DailyPoemDiscussionQueryCriteria;
import me.zhengjie.dailyPoem.mapper.DailyPoemDiscussionMapper;
import me.zhengjie.dailyPoem.service.DailyPoemDiscussionService;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;

@Service
@RequiredArgsConstructor
public class DailyPoemDiscussionServiceImpl extends ServiceImpl<DailyPoemDiscussionMapper, DailyPoemDiscussion> implements DailyPoemDiscussionService {

    private final DailyPoemDiscussionMapper discussionMapper;

    @Override
    public PageResult<DailyPoemDiscussion> queryAll(DailyPoemDiscussionQueryCriteria criteria, Page<Object> page) {
        IPage<DailyPoemDiscussion> pageEntities = discussionMapper.findAll(criteria, page);
        return PageUtil.toPage(pageEntities);
    }

    @Override
    public List<DailyPoemDiscussion> queryAll(DailyPoemDiscussionQueryCriteria criteria) {
        return discussionMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer create(DailyPoemDiscussion resources) {
        save(resources);
        return resources.getDiscussionId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DailyPoemDiscussion resources) {
        DailyPoemDiscussion existing = getById(resources.getDiscussionId());
        if (existing != null) {
            BeanUtil.copyProperties(resources, existing);
            existing.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
            updateById(existing);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<DailyPoemDiscussion> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DailyPoemDiscussion entity : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("关联的诗歌ID", entity.getPoemId());
            map.put("用户ID", entity.getUserId());
            map.put("用户名", entity.getUserName());
            map.put("评论内容", entity.getContent());
            map.put("点赞数", entity.getLikeCount());
            map.put("子评论数", entity.getCommentCount());
            map.put("是否置顶", entity.getIsPinned());
            map.put("状态：1=正常，0=删除，2=审核中", entity.getStatus());
            map.put("创建时间", entity.getCreatedAt());
            map.put("更新时间", entity.getUpdatedAt());
            list.add(map);
        }
        me.zhengjie.utils.FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional
    public void togglePin(Integer id) {
        DailyPoemDiscussion entity = getById(id);
        if (entity != null) {
            Integer current = entity.getIsPinned();
            entity.setIsPinned(Objects.equals(current, 1) ? 0 : 1);
            updateById(entity);
        }
    }

    @Override
    @Transactional
    public void auditDiscussion(Integer id, Integer status) {
        DailyPoemDiscussion entity = getById(id);
        if (entity != null) {
            entity.setStatus(status);
            updateById(entity);
        }
    }

    @Override
    @Transactional
    public void like(Integer id) {
        DailyPoemDiscussion entity = getById(id);
        if (entity != null) {
            entity.setLikeCount(Optional.ofNullable(entity.getLikeCount()).orElse(0) + 1);
            updateById(entity);
        }
    }

}
